package com.excletojson;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        EasyExcelListener listener = new EasyExcelListener();
        ExcelReader excelReader = EasyExcelFactory.read("dic_dp+ln.xlsx", ExcelEntity.class, listener)
                .headRowNumber(1)
                .ignoreEmptyRow(false)//不忽略空行 默认是忽略空行
                .build();
        excelReader.readAll();
        List<ExcelEntity> datas = listener.getDatas();

        HashMap<String, HashMap<String, String>> hashMap = new HashMap<>();
        for (ExcelEntity data : datas) {
            HashMap<String, String> map = new HashMap<>();
            map.put("field", data.getField());
            map.put("notes", data.getNotes());
            map.put("javatype", data.getJavatype());
            map.put("leng", data.getLeng());
            map.put("accuracy", data.getAccuracy());
            hashMap.put(data.getField(), map);
        }

        //String dicStr = JSONObject.parse(JSON.toJSONString(datas)).toString();
        String dicStr = JSONObject.parse(JSON.toJSONString(hashMap)).toString();

        OutputStream output = new FileOutputStream("dic_dp+ln.json");
        IOUtils.write(dicStr, output);


    }
}
